/**
 * 
 */
package x10.eventServices.gui;

import java.util.logging.Logger;

import x10.event.X10Event;
import x10.eventServices.EventServiceManager;
import x10.eventServices.X10EventListener;
import x10.properties.PropertiesManager;

/**
 * @author Denny Sheirer
 * @date   2011
 */
public class GUIManager implements X10EventListener, Runnable {
    String eventListenerName = "GUIManager";
    Logger logger = Logger.getLogger(this.getClass().getName());
    EventServiceManager eventServiceManager;
    PropertiesManager properties;
    GUI gui;
    X10Event lastReceived = null;
    boolean guiStarted = false;
    
    public GUIManager(EventServiceManager serviceManager, PropertiesManager properties){
	this.eventServiceManager = serviceManager;
	this.properties = properties;
    }

    /**
     * getEventListenerName() - returns the name of this X10EventListener class
     */
    public String getEventListenerName(){
	return eventListenerName;
    }
    
    public void run(){
	logger.fine("Starting the GUI");
	gui = new GUI(this, properties);
	guiStarted = true;
    }
    
    public void receiveEvent(X10Event device){
	boolean add = false;
	
	if(lastReceived == null){
	    lastReceived = device;
	    add = true;
	} else if(device.equals(lastReceived)){
	    if(gui.isSuppressDuplicateLogEntries()){
		add = false;
	    } else {
		add = true;
	    }
	} else {
	    lastReceived = device;
	    add = true;
	}
	
	if(add){
            addToLog("RCVD: " + device.toString());
	}
    }
    
    public void addToLog(String message){
	if(guiStarted){
	    gui.addToLogTextArea(message);
	}
    }

    public void transmitEvent(X10Event device){
	eventServiceManager.receiveEvent(device);
    }
    
}
